const { writeComment, findOne } = require('../models/article')

const commentWrite = async (req, res, next) => {
    res.set('content-type', 'application/json; charset=utf-8')
    const { c_content, article, c_author, reply } = req.body

    const result = await writeComment(c_author, article, c_content, reply)

    if(result) {
        res.render('succ', {
            data: JSON.stringify({
                msg: result
            })
        })
    } else {
        res.render('fail')
    }
}

const comment = async (req, res, next) => {
    res.set('content-type', 'application/json; charset=utf-8')
    const id = req.params.id
    const blog = await findOne(id)
    //id错了的话会报错 要加promiss
    if(blog) {
        const comments = blog.comment
        res.render('succ', {
            data: JSON.stringify(comments)
        })
    } else {
        res.render('fail')
    }

}

module.exports = {
    commentWrite,
    comment,
}